package com.kgame5.xtank3d.app.gamecore.pane.clock {
import kgame5.kgui.gameui.digi.SmartDigiStrip;
import kgame5.kutil.KEvent;

import com.kgame5.xtank3d.resource.ResourceManagerX;

import flash.display.MovieClip;
import flash.events.EventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Timer;

/**
 * @author kangkai
 * TODO 这个要做成通用的！
 */
public class ClockComp extends EventDispatcher{
	public static const EVENTTYPE_TIMEOVER:String="EVENTTYPE_TIMEOVER";
	
	private var 
		mc:MovieClip;
			
	private var sd_min:SmartDigiStrip;
	private var sd_sec:SmartDigiStrip;
	
	private var timer:Timer;
	
	private var leftSec:int;
	
	public function ClockComp(p_mc:MovieClip){
		mc=p_mc;
		
		sd_min=new SmartDigiStrip(mc.mc_min,"MyMc_DigiTime",2,false);
		sd_sec=new SmartDigiStrip(mc.mc_sec,"MyMc_DigiTime",2,false);
		
		timer=new Timer(1000);
	}
	public function Init():void{
		//T.p(this,"-------Start",p_leftSec);
		leftSec=ResourceManagerX.getInstance().getLevelVO().time;
		updateView();//在最开始是需要updateView一下的,因为timer启动之后需要一段时间才会onTimer()呢
	}
	public function Start():void{
		
		timer.addEventListener(TimerEvent.TIMER, onTimer);
		timer.start();
	}
	
	private function onTimer(e:TimerEvent):void{
		leftSec--;
		
		updateView();
		
		//T.p("sec:"+showedSec,"min:"+showedMin);
		if(leftSec==0){
			Clear();
			notifyTimeOver();
		}
	}
	
	public function Stop():void{
		timer.stop();
	}
	
	public function Clear():void{
		//T.p(this,"--------Clear");
		
		leftSec=0;
		updateView();
		
		timer.removeEventListener(TimerEvent.TIMER, onTimer);
		timer.stop();
	}
	
	private function notifyTimeOver():void{
		this.dispatchEvent(new KEvent(EVENTTYPE_TIMEOVER));
	}
	
	//被外界调用，更改自己的显示时间
	private function updateView():void{
		//T.p(this,"updateView");
		var myMin:int=leftSec/60;//分钟
		var mySec:int=leftSec%60;//秒
		
		sd_min.update(myMin);
		sd_sec.update(mySec);
	}
	
}//end of class
}
